package ebcs.scan.controller.commentCtr;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ebcs.database.bean.BsComment;
import ebcs.database.bean.BsGroupInfor;
import ebcs.database.service.BsCommentServiceImp;
import ebcs.database.service.impl.BsCommentServiceImpl;
import ebcs.scan.controller.batch.BatchManagerCtrl;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.tools.string.DateUtil;


@Controller
public class CommentCtrl {
	protected JAppContextFactory serviceFactory = new JAppContextFactory();
	@Autowired
	BsCommentServiceImp bsCommentServiceImp;
	@Autowired
    BatchManagerCtrl batch;
	
	/**
	 * 团队经理查询等待评价的需求
	 * @param request
	 * @param response
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/searchWaitCommentNeed.do")
	public void searchWaitCommentNeed(HttpServletRequest request,HttpServletResponse response) throws JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		BsComment param = context.obtainReqModel(BsComment.class);
		List<BsComment> list = bsCommentServiceImp.findWaitCommentNeed(param);
		context.buildAppResponse(list);
	}
	
	/**
	 * 将用户选中的需求添加到评价表中
	 * @param list
	 */
	@RequestMapping("/page/main/addNeedToComment.do")
	@ResponseBody
	public String addNeedToComment(@RequestBody List<BsComment> list,HttpSession session){
		//获取时间戳
		String dateStr = DateUtil.formatDateToStr(new Date(), "yyyyMMddHHmmss");
		for(BsComment bc:list){
			bc.setDateStr(dateStr);
		}
		//批量添加到数据库
		int i = bsCommentServiceImp.insertBatch(list);
		//推送给当前登录的团队经理
		BsGroupInfor loginGroupInfo =  ((List<BsGroupInfor>) session.getAttribute("loginGroupInfo")).get(0);
		//获取当前登录的团队经理的openid
		String openid = loginGroupInfo.getOpenid();
		batch.sendWaitNeedToGroupUser(dateStr, loginGroupInfo.getGroupManger(), openid, list.size());
		if(i > 0){
			return "{\"status\":\"success\"}";
		}else{
			return "{\"status\":\"faild\"}";
		}
	}
	
	/**
	 * 根据dateStr查询待评价的需求
	 * @param dateStr
	 * @return
	 */
	@RequestMapping("/page/main/findWaitCommentByDateStr.do")
	public String findWaitCommentByDateStr(HttpServletRequest request,String dateStr){
		List<BsComment> list = this.bsCommentServiceImp.findWaitCommentByDateStr(dateStr);
		//System.out.println(list);
		request.setAttribute("comList", list);
		return "main/report/comment";
	}
	
	@RequestMapping("/page/main/updateBatchByRouid.do")
	@ResponseBody
	public String updateBatchByRouid(@RequestBody List<BsComment> list){
		int row = bsCommentServiceImp.updateBatchByRouid(list);
		System.out.println(row);
		if(row == 0){
			return "{\"status\":\"faild\"}";
		}else{
			return "{\"status\":\"success\"}";
		}
	}
}
